package models;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import org.codehaus.jackson.annotate.JsonIgnore;

import play.db.ebean.Model;

@Entity
public class OpenTokSession extends Model{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Long id;
	
	public String sessionId;

	@Lob
	public String token;
	@Lob
	public String publisherToken;
	@Lob
	public String subscriberToken;
	@Lob
	public String moderatorToken;

	@Transient
	public Integer apiKey;
	
	public String status;
	
	@OneToOne
	@JsonIgnore
	public Event event;
	
	public static Finder<Long,OpenTokSession> find = new Finder<Long,OpenTokSession>(Long.class, OpenTokSession.class);
	
	public static OpenTokSession findByEventId(Long eventId){
		 OpenTokSession openTokSession = null;
		 
		 Event event = new Event();
		 event.id = eventId;
		 List<OpenTokSession> openTokSessionsList = find.where().eq("event", event).findList();
		 if(openTokSessionsList != null && openTokSessionsList.size() > 0 ){
			 openTokSession = openTokSessionsList.get(0);
		 }
		 return openTokSession;
	}
	
	public static OpenTokSession findById(Long id){
		 OpenTokSession openTokSession = null;
		 
		 openTokSession = find.where().eq("id", id).findUnique();
		 return openTokSession;
	}
	
	public static OpenTokSession findBySessionId(Long sesionId){
		 OpenTokSession openTokSession = null;
		 
		 List<OpenTokSession> openTokSessionsList = find.where().eq("id", sesionId).findList();
		 if(openTokSessionsList != null && openTokSessionsList.size() > 0 ){
			 openTokSession = openTokSessionsList.get(0);
		 }
		 return openTokSession;
	}
}
